<?php 
header("Content-Type:text/html;charset=utf-8");
include 'includes/init.php';
include_once 'includes/upload.class.php';

session_start();

$id=isset($_SESSION['id'])?$_SESSION['id']:'';
$nphone = isset($_SESSION['phone']) ? $_SESSION['phone'] : '';
$uesrname = isset($_SESSION['uesrname']) ? $_SESSION['uesrname'] : '';
$url = $_SERVER['HTTP_HOST'];
$time = time();


// 具体项目数据
if(!empty($_GET['project'])){
    error_log('step project');
	$project = $_GET['project'];
	$code = $_GET['code'];

	$sql = "SELECT
			e.*, m.company_name
		FROM
			t_examine AS e,
			t_member AS m
		WHERE
			(
				m.company_data = e.company_data
				AND e.auditing = 1
				AND e.project = '".$project."'
				AND e.code = '".$code."'
			)
		OR (
			m.company_data = e.company_data
			AND e.auditing = 2
			AND e.project = '".$project."'
			AND e.code = '".$code."'
		)
		ORDER BY
			e.id DESC";
			$infoAll = $db_Obj->getall($sql);
// 对比旧数据 旧数据与提交数据对比删除旧数据的重复部分.
 	$sql = "SELECT
		    m.company_name,
		    m.company_data,
		    p.mid,
		    p.nphone,
		    p.project,
		    p.status,
		    p.price_type,
		    p.lasttime,
		    p.units,
		    p.currency,
		    p.number,
		    p.material,
			p.purchase,
			p.plant,
			p.extranum,
		    b.price,
		    b.btype,
		    b.bsize,
		    b.delivery,
		    b.MOQ,
		    b.MPQ,
		    b.createtime,
		    b.bnumber,
		    b.rate,
		    b.id
		FROM
		    t_bid AS b,
		    t_member AS m,
		    t_product AS p
		WHERE
		(
				p.mifeng = 0 
				AND p.project = '".$project."'
				AND b.mid = m.id
				AND p.type = b.btype
			)
		OR (
			p.mifeng = 1
			AND p.lasttime < ".$time."
			AND p.project = '".$project."'
			AND b.mid = m.id
			AND p.type = b.btype
		)	
		ORDER BY p.type asc, b.price asc
		";
	$infoAll_bid = $db_Obj->getall($sql);
	foreach ($infoAll as $k_a => $v_a) {
		foreach ($infoAll_bid as $k_b => $v_b) {
			if($v_a['bid_id'] == $v_b['id']){
				unset($infoAll_bid[$k_b]);
			}			
		}
	}
	$new_infoAll = array_merge($infoAll, $infoAll_bid);

	// code 可以区分一个项目一个记录
	$code = $new_infoAll[0]['code'];
	// 记录
	$sql = "select * from t_exa_log where project = '".$project."' and code = '".$code."' ";
	$exa_data = $db_Obj->getall($sql);
}

// 审核部分
if(!empty($_POST)){
    error_log('step check');
    //取出项目审批人员组
    $project = $_POST['project'];
    $check_member_sql = "select check_member from t_examine where project = '$project' and code = '$code'";
    error_log('sql: '.$check_member_sql);
    $check_member_list = $db_Obj->getall($check_member_sql);
    $check_member_arr = explode(';',$check_member_list[0]['check_member']);
    array_pop($check_member_arr);

    if (in_array($uesrname,$check_member_arr)){
        error_log('into 1');
        $project = $_POST['project'];
        $auditing = $_POST['auditing'];
        $opinion = $_POST['opinion'];
        // admin mid
        $mid = $_POST['mid'];


        if($auditing == 0){
            error_log('通过');
            //弹出本审批人
            foreach ($check_member_arr as $item=>$value){
                if ($uesrname == $value) {
                    array_splice($check_member_arr, $item, 1);
                }
            }
            //下一位审批者
            @$next = $check_member_arr[0];
            //最会一位审批者
            $last = end($check_member_arr);
            //组装check_member
            $c_m = '';
            foreach ($check_member_arr as $val){
                $c_m = $c_m.$val.";";
            }


            //判断是否是最后的审批者
            if (sizeof($check_member_arr) == 0){
                error_log('check_member 1');
                $sql = "update t_examine set auditing = '0' , opinion = '".$opinion."' , progress = '归档人', check_member = "."''"." where project = '".$project."' and progress = '".$uesrname."'  and code = '".$code."'";
            } else {
                error_log('check_member 2');
                $sql = "update t_examine set auditing = '1' , opinion = '".$opinion."', progress = '$next', check_member = "."'$c_m' "." where project = '".$project."' and progress = '".$uesrname."'  and code = '".$code."'";
            }
            $query = $db_Obj->query($sql);
            if($query == true){
                // LOG 记录
                $sql = "insert into t_exa_log (project, status, time, opinion, name, code) values('".$project."', 0, '".time()."', '".$opinion."', '".$uesrname."', '".$code."') ";
                $db_Obj->query($sql);
                echo "<script>alert('通过成功~');window.location.href='http://".$url."/dai-process.php'</script>";
            }
        }
        if($auditing == 1){
            error_log('驳回');
            // 查出创建人
            $sql = "select names from t_member where id = '".$mid."' ";
            $member = $db_Obj->getone($sql);

            // 驳回;
            $sql = "update t_examine set auditing = '2' , opinion = '".$opinion."' , progress = '".$member['names']."',check_member = ''  where project = '".$project."' and progress = '".$uesrname."' and code = '".$code."'  ";
            $query = $db_Obj->query($sql);
            if($query == true){
                // LOG 记录
                $sql = "insert into t_exa_log (project, status, time, opinion, name, code) values('".$project."', 1, '".time()."', '".$opinion."', '".$uesrname."', '".$code."') ";
                $db_Obj->query($sql);

                echo "<script>alert('驳回成功~');window.location.href='http://".$url."/dai-process.php'</script>";
            }
        }
    }

    error_log('step if out');
}
    error_log('out');
include 'dai-pro-detail.html';

 ?>